<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="auth" uri="http://www.jiyinit.com/authTag"%>
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="${basePath}/assets/css/foundation-datepicker.css"/>
<script type="text/javascript" src="${basePath}/assets/js/foundation-datepicker.js"></script>

</head>
<body>
<div class="right-container">
    <div class="container-select text-left">
        <div class="search-input">
            <span>任务名称：</span>
            <input id="title" name="title" type="text">
        </div>
        <div class="search-input">
            <span>发起人：</span>
            <input id="userName" name="userName" type="text">
        </div>
		<div class="search-input">
            <span>发起日期：</span>
            <input type="text" id="create_time" name="create_time" class="date-icon"/>
        </div>
        <button id="searchBtn" class="btn btn-primary" type="button">
            <i class="fa fa-search"></i>&nbsp;查询
        </button>
    </div>
    <div class="manage-button text-left">
        <auth:auth authUrl="task/addTask">
            <button id="resolveTaskBtn" class="btn btn-primary" type="button">
                <i class="fa fa-key"></i>&nbsp;任务分解
            </button>
        </auth:auth>
        <auth:auth authUrl="task/taskAllot">
        <button id="allotTaskBtn" class="btn btn-primary" type="button">
            <i class="fa fa-key"></i>&nbsp;任务分配
        </button>
        </auth:auth>
        <auth:auth authUrl="userTask/userTaskLists">
        <button id="allotTaskDetailBtn" class="btn btn-primary" type="button">
            <i class="fa fa-key"></i>&nbsp;任务分配列表
        </button>
        </auth:auth>
        <auth:auth authUrl="taskMessage/message">
        <button id="taskMessageBtn" class="btn btn-primary" type="button">
            <i class="fa fa-key"></i>&nbsp;任务说说
        </button>
        </auth:auth>
    </div>
    <div class="row">
        <div class="col-xs-12">

            <table id="grid-table"></table>

            <div id="grid-pager"></div>

        </div>
    </div>
</div>

<!-- page specific plugin scripts -->
<script src="${basePath}/assets/js/jqGrid/jquery.jqGrid.min.js${VERSION}"></script>
<script src="${basePath}/assets/js/jqGrid/grid.locale-cn.js${VERSION}"></script>

<!-- inline scripts related to this page -->
<script type="text/javascript">
    var pageii = null;
    var taskTitle;
    var taskId;
    var createUid;
    var createUname;
    var createTime;
    var subgrid_table;
    var rowId_flag=0;
    var startTimePl;
    var endTimePl;
    
    jQuery(function ($) {
        $('.date-icon').fdatepicker({
            format: 'yyyy-mm-dd'
        });

       
    	
      /*  CommonUtils.timeout("#error");*/
        CommonUtils.grid({
            url: "${basePath}/task/findMyAllTasks",
            
            colNames: ['序号','任务名称', '发起人','发起人ID','任务种类','任务类型', '计划开始时间','实际开始时间', '计划结束时间','实际结束时间','分配时间'],
            colModel: [
                {name: 'taskId', index: 'id', key: true, width: 30, sorttype: "int"},
                {
                    name: 'title',
                    index: 'title',
                    width: 50
                },
                {
                    name: 'userName',
                    index: 'userName',
                    width: 50
                },{
                    name: 'createUid',
                    index: 'createUid',
                    width: 50,
                    hidden:true
                },
                {
                    name: 'taskCla',
                    index: 'taskCla',
                    width: 50,
                    formatter: CommonUtils.taskCla
                },
                {
                    name: 'taskType',
                    index: 'taskType',
                    width: 50,
                    formatter: CommonUtils.taskType
                },
                {
                    name: 'startTimePl',
                    index: 'startTimePl',
                    width: 70,
                    formatter: CommonUtils.formateDay
                },
                {
                    name: 'startTimeReal',
                    index: 'startTimeReal',
                    width: 70,
                    formatter: CommonUtils.formateDay
                },
                {
                    name: 'endTimePl',
                    index: 'endTimePl',
                    width: 70,
                    formatter: CommonUtils.formateDay
                },
                {
                    name: 'endTimeReal',
                    index: 'endTimeReal',
                    width: 70,
                    formatter: CommonUtils.formateDay
                },
                {
                    name: 'createTime',
                    index: 'createTime',
                    width: 80,
                    formatter: CommonUtils.formateDate
                }
            ],
            
            /*
            subGrid : true,
            subGridRowExpanded: function(subgrid_id, row_id) {
                   var subgrid_table_id;
                   subgrid_table_id = subgrid_id+"_t";
                   jQuery("#"+subgrid_id).html("<table id='"+subgrid_table_id+"' class='scroll'></table>");
                   //alert("${basePath}/task/findByPage"+row_id);
                   
                   subgrid_table = subgrid_table_id;
                   
                   jQuery("#"+subgrid_table_id).jqGrid({
                      url:"${basePath}/task/findMyAllTasks?taskPid="+row_id,
                      datatype: "json",
                      mtype: 'POST',
                      multiselect: true,
                      colNames: ['序号','任务名称', '发起人','任务种类','任务类型', '计划开始时间','实际开始时间', '计划结束时间','实际结束时间','创建时间'],
                      colModel: [
                        {name: 'taskId', index: 'id', key: true, width: 60, sorttype: "int"},
                		{name: 'title',index: 'title',width: 90},
		                {name: 'userName',index: 'userName',width: 50},
		                {name: 'taskCla',index: 'taskCla',width: 100,formatter: CommonUtils.taskCla},
		                {name: 'taskType',index: 'taskType',width: 100,formatter: CommonUtils.taskType},
		                {name: 'startTimePl',index: 'startTimePl',width: 120,formatter: CommonUtils.formateDay},
		                {name: 'startTimeReal',index: 'startTimeReal',width: 120,formatter: CommonUtils.formateDay},
		                {name: 'endTimePl',index: 'endTimePl',width: 120,formatter: CommonUtils.formateDay},
		                {name: 'endTimeReal',index: 'endTimeReal',width: 120,formatter: CommonUtils.formateDay},
		                {name: 'createTime',index: 'createTime',width: 140,formatter: CommonUtils.formateDate}
                      ],
                      height: '100%',
                      rowNum:20,
                      sortname: 'num',
                      sortorder: "asc"
                   });
                   rowId_flag = 1;
               },
            
            
            */
            
            jsonReader: {
                root: "rows", page: "currentPage", total: "totalPages",
                records: "totalRecords", repeatitems: false, id: "id"
            }
        });

      
       
      
      
        $("#searchBtn").click(function () {
            query();
        });

        $("#userName").keypress(function (e) {
            if (e.which == 13) {
                query();
            }
        });

        function query() {
            var queryData = {
                "userName": $.trim($('#userName').val()),
                "title": $.trim($('#title').val()),
                "createTime": $.trim($('#create_time').val())
            };
			//alert($.trim($('#create_time').val()));
            $("#grid-table").jqGrid("setGridParam",
                    {
                        postData: queryData,
                        datatype: 'json'
                    }
            ).trigger("reloadGrid", [{page: 1}]);
            
           
        };

        $("#addBtn").click("click", function () {
            if ($(window).width() > 800){
                pageii = layer.open({
                    title: "添加任务",
                    type: 2,
                    maxmin: true,
                    area: ["560px", "70%"],
                    content: ['${basePath}/task/add']
                });
            }
            else{
                pageii = layer.open({
                    title: "添加任务",
                    type: 2,
                    maxmin: true,
                    area: ["80%", "90%"],
                    content: ['${basePath}/task/add']
                });
            }
        });



        
        
        
        var loading = null;

        $('#deleteBtn').click("click", function () {
        	alert("#"+subgrid_table);
        	var rowsIds_flag = 0;
        	var rowsIds;
        	var rowsIds_s;
        	var rowsIds_t = $("#grid-table").jqGrid('getGridParam', 'selarrrow');
        	if(rowId_flag==1){
        		rowsIds_s =  $("#"+subgrid_table).jqGrid('getGridParam', 'selarrrow');
        		if(rowsIds_s == ""&&rowsIds_t == ""){
            		layer.msg("请选择一行数据进行删除！");
                 	return;
            	}else if(rowsIds_t != "" && rowsIds_s == ""){
            		rowsIds_flag = 1;
            	}else if(rowsIds_t == "" && rowsIds_s != ""){
            		rowsIds_flag = 2;          		
            	}else{
            		rowsIds_flag = 3;
            	}
        	}else{
        		if (rowsIds_t.length > 1  || rowsIds_t == "") {
                    layer.msg("请至少选择一行数据进行删除！");
                    return;
                }
        		rowsIds_flag = 1; 
        	}
           alert("rowsIds_flag="+rowsIds_flag);
        	
            layer.confirm('是否删除选中数据？',{ icon: 3}, function (index) {
                var taskIds = [];
                var rowData;
                switch(rowsIds_flag){
                	case 3:
                		$(rowsIds_t).each(function () {
                            rowData = $("#grid-table").jqGrid('getRowData', this);
                            taskIds.push(rowData.taskId);
                        });
                		alert("taskIds.length="+taskIds);
                		$(rowsIds_s).each(function () {
                            rowData = $("#"+subgrid_table).jqGrid('getRowData', this);
                            taskIds.push(rowData.taskId);
                        });
                		alert("taskIds.length="+taskIds);
                		break;
                	case 2:
                		$(rowsIds_s).each(function () {
                            rowData = $("#"+subgrid_table).jqGrid('getRowData', this);
                            taskIds.push(rowData.taskId);
                        });
                		alert("taskIds.length="+taskIds);
                		break;
                	case 1:
                		$(rowsIds_t).each(function () {
                            rowData = $("#grid-table").jqGrid('getRowData', this);
                            taskIds.push(rowData.taskId);
                        });
                		alert("taskIds.length="+taskIds);
                		break;
                }
                
                
				
                $.ajax({
                    type: "post",
                    data: "taskIds=" + taskIds,
                    url: "${basePath}/task/delete",
                    dataType: "json",
                    beforeSend:function () {
                        $('#deleteBtn').prop("disabled", true);
                        loading = layer.load(0, {
                            shade: [0.4,'#fff'] //0.4透明度的白色背景
                        });
                    },
                    success: function (data) {
                        $('#deleteBtn').prop("disabled", false);
                        layer.close(loading);
                        if (data.status == "0") {
                            layer.msg("删除成功！");
                            $("#grid-table").jqGrid().trigger("reloadGrid", [{page: 1}]);
                        }
                        else {
                            layer.alert(data, {
                                icon: 0,
                                skin: 'layui-layer-lan',
                                closeBtn: 0
                            })
                        }
                    },
                    error: function(xhr, textStatus, errorThrown) {
                        $('#deleteBtn').prop("disabled", false);
                        layer.close(loading);
                    }
                });
                return false;

            });

        });

        
        $("#allotTaskBtn").click("click", function () {
            //var rowId = $("#grid-table").jqGrid('getGridParam', 'selarrrow');
			var subFlag = 0;
			
			var rowData;
            var rowId_t = $("#grid-table").jqGrid('getGridParam', 'selarrrow');
            var rowId_s;
            
            if(rowId_flag==1){
            	rowId_s = $("#"+subgrid_table).jqGrid('getGridParam', 'selarrrow');
            	
            	//alert(rowId_t);
                //alert(rowId_s);
            	if(rowId_s != ""&&rowId_t != ""){
            		layer.msg("请选择一行数据进行分配！");
                 	return;
            	}else if(rowId_t != "" && rowId_s == ""){
            		if(rowId_t.length > 1){
            			layer.msg("请选择一行数据进行分配！");
                     	return;
            		}
            		rowData = $("#grid-table").jqGrid('getRowData', rowId_t);
            	}else if(rowId_t == "" && rowId_s != ""){
            		if(rowId_s.length > 1){
            			layer.msg("请选择一行数据进行分配！");
                     	return;
            		}
            		rowData = $("#"+subgrid_table).jqGrid('getRowData', rowId_s);
            	}else{
            		layer.msg("请选择一行数据进行分配！");
                 	return;
            	}
            }else{
            	if (rowId_t.length > 1|| rowId_t == "") {
                    layer.msg("请选择一行数据进行分配！");
                    return;
                }
            	rowData = $("#grid-table").jqGrid('getRowData', rowId_t);
            }
            
            //var rowData = $("#grid-table").jqGrid('getRowData', rowId);
            taskTitle = rowData.title;
            taskId = rowData.taskId;
            //alert(taskTitle);
            startTimePl = rowData.startTimePl;
            endTimePl =  rowData.endTimePl;
            
            pageii = layer.open({
                title: "任务分配",
                type: 2,
                area: ["800px", "500px"],
                content: '${basePath}/task/taskAllot'
            });
        });
        
       
        $("#allotTaskDetailBtn").click("click", function () {
			var subFlag = 0;
			var rowData;
            var rowId_t = $("#grid-table").jqGrid('getGridParam', 'selarrrow');
            var rowId_s;
            
            if(rowId_flag==1){
            	rowId_s = $("#"+subgrid_table).jqGrid('getGridParam', 'selarrrow');
            	
            	//alert(rowId_t);
                //alert(rowId_s);
            	if(rowId_s != ""&&rowId_t != ""){
            		layer.msg("请选择一行数据进行查看！");
                 	return;
            	}else if(rowId_t != "" && rowId_s == ""){
            		if(rowId_t.length > 1){
            			layer.msg("请选择一行数据进行查看！");
                     	return;
            		}
            		rowData = $("#grid-table").jqGrid('getRowData', rowId_t);
            	}else if(rowId_t == "" && rowId_s != ""){
            		if(rowId_s.length > 1){
            			layer.msg("请选择一行数据进行查看！");
                     	return;
            		}
            		rowData = $("#"+subgrid_table).jqGrid('getRowData', rowId_s);
            	}else{
            		layer.msg("请选择一行数据进行查看！");
                 	return;
            	}
            }else{
            	if (rowId_t.length > 1  || rowId_t == "") {
                    layer.msg("请选择一行数据进行查看！");
                    return;
                }
            	rowData = $("#grid-table").jqGrid('getRowData', rowId_t);
            }
            taskTitle = rowData.title;
            taskId = rowData.taskId;
            //alert(taskId);
            
            pageii = layer.open({
                title: "任务分配列表",
                type: 2,
                area: ["1000px", "600px"],
                content: '${basePath}/userTask/userTaskLists?taskId='+taskId
                	     
            });
        });
        
        
        
        $("#resolveTaskBtn").click("click", function () {
        	var rowId = $("#grid-table").jqGrid('getGridParam', 'selarrrow');
        	if (rowId.length > 1 || rowId == "") {
                layer.msg("请选择一任务进行分解！");
                return;
            }
            var rowData = $("#grid-table").jqGrid('getRowData', rowId);
            taskTitle = rowData.title;
            taskId = rowData.taskId;
            var taskPuid = rowData.createUid;
            //alert('${basePath}/task/add?taskId='+taskId);
            if ($(window).width() > 800){
                pageii = layer.open({
                    title: "任务分解",
                    type: 2,
                    maxmin: true,
                    area: ["560px", "70%"],
                    content: ['${basePath}/task/add?taskId='+taskId+"&title="+title+"&taskPuid="+taskPuid]
                });
            }
            else{
                pageii = layer.open({
                    title: "任务分解",
                    type: 2,
                    maxmin: true,
                    area: ["80%", "90%"],
                    content: ['${basePath}/task/add?taskId='+taskId+"&title="+title+"&taskPuid="+taskPuid]
                });
            }
        });
        
        
$("#taskMessageBtn").click("click", function () {
        	
			var subFlag = 0;
			var rowData;
            var rowId_t = $("#grid-table").jqGrid('getGridParam', 'selarrrow');
            var rowId_s;
            if(rowId_flag==1){
            	rowId_s = $("#"+subgrid_table).jqGrid('getGridParam', 'selarrrow');
            	if(rowId_s != ""&&rowId_t != ""){
            		layer.msg("请选择一行数据进行说说！");
                 	return;
            	}else if(rowId_t != "" && rowId_s == ""){
            		if(rowId_t.length > 1){
            			layer.msg("请选择一行数据进行说说！");
                     	return;
            		}
            		rowData = $("#grid-table").jqGrid('getRowData', rowId_t);
            	}else if(rowId_t == "" && rowId_s != ""){
            		if(rowId_s.length > 1){
            			layer.msg("请选择一行数据进行说说！");
                     	return;
            		}else{
            			layer.msg("子任务不能进行说说！");
                     	return;
            		}
            	}else{
            		layer.msg("请选择一行数据进行说说！");
                 	return;
            	}
            }else{
            	if (rowId_t.length >1  || rowId_t == "") {
                    layer.msg("请选择一行数据进行说说！");
                    return;
                }
            	rowData = $("#grid-table").jqGrid('getRowData', rowId_t);
            }
            
            taskTitle = rowData.title;
            taskId = rowData.taskId;
            var title = rowData.title;
            createUid = rowData.createUid;
            createUname = rowData.userName;
            createTime = rowData.createTime;
            if ($(window).width() > 800){
                pageii = layer.open({
                    title: "任务说说",
                    type: 2,
                    maxmin: true,
                    area: ["560px", "95%"],
                    content: ['${basePath}/taskMessage/message?taskId='+taskId+'&taskUid='+createUid]
                });
            }
            else{
                pageii = layer.open({
                    title: "任务说说",
                    type: 2,
                    maxmin: true,
                    area: ["80%", "95%"],
                    content: ['${basePath}/taskMessage/message?taskId='+taskId+'&taskUid='+createUid]
                });
            }
        });
    });
</script>
</body>
</html>